package com.aba.dormitory.system.student;
import java.util.HashMap;
import java.util.Map;

public class StudentManager {
    private static Map<String, Student> students = new HashMap<>();
    private static Map<String, String> studentTeacherMapping = new HashMap<>();

    // 注册新学生
    public static boolean registerStudent(String studentId, String teacherId, String password, 
                                        String name, int age, String gender, 
                                        String securityQuestion, String securityAnswer) {
        if (students.containsKey(studentId)) {
            return false;
        }
        
        // 验证教师是否存在（简化处理）
        if (!isValidTeacher(teacherId)) {
            return false;
        }
        
        Student student = new Student(studentId, teacherId);
        boolean success = student.register(password, name, age, gender, securityQuestion, securityAnswer);
        if (success) {
            students.put(studentId, student);
            studentTeacherMapping.put(studentId, teacherId);
        }
        return success;
    }

    // 学生登录
    public static Student login(String studentId, String password) {
        Student student = students.get(studentId);
        if (student == null) {
            return null;
        }
        if (student.authenticate(password)) {
            return student;
        }
        return null;
    }

    // 重置密码
    public static boolean resetPassword(String studentId, String securityAnswer, String newPassword) {
        Student student = students.get(studentId);
        if (student == null) {
            return false;
        }
        return student.resetPassword(securityAnswer, newPassword);
    }

    // 获取学生信息（仅限本人或管理员）
    public static Student getStudent(String studentId, String requesterId, boolean isAdmin) {
        if (!isAdmin && !studentId.equals(requesterId)) {
            return null; // 非管理员不能查看他人信息
        }
        return students.get(studentId);
    }

    // 设置寝室成绩
    public static void setDormitoryScore(String studentId, String semester, double score) {
        Student student = students.get(studentId);
        if (student != null) {
            student.addDormitoryScore(semester, score);
        }
    }

    // 完善电子档案
    public static void completeProfile(String studentId, EmergencyContact contact) {
        Student student = students.get(studentId);
        if (student != null) {
            student.completeProfile(contact);
        }
    }

    // 验证教师是否存在（简化处理）
    private static boolean isValidTeacher(String teacherId) {
        // 实际应用中应该查询教师数据库
        return teacherId != null && !teacherId.isEmpty();
    }
}    
